﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            if (Session["WelcomeUser"] != null)
            {
                if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
                {
                    // Ko chay lai doan nay khi postback
                    if (!IsPostBack)
                    {
                        //Lay id truyen tu querry string
                        int id = Convert.ToInt32(Request.QueryString["id"].ToString());

                        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

                        // Load thong tin event co id tren vao form
                        DataSet ds = new DataSet();
                        ds = udm.showEventsByID(udm.createConnection(), id);

                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            string title = ds.Tables[0].Rows[0][1].ToString();
                            DateTime start = Convert.ToDateTime(ds.Tables[0].Rows[0][2].ToString());
                            DateTime end = Convert.ToDateTime(ds.Tables[0].Rows[0][3].ToString());
                            string location = ds.Tables[0].Rows[0][4].ToString();
                            string content = ds.Tables[0].Rows[0][6].ToString();

                            // Luu link image len session de khi nguoi dung ko upload file moi thi dung luon link nay
                            Session["fileUpload"] = ds.Tables[0].Rows[0][5].ToString();

                            // Bat dau do vao form
                            txtTitle.Text = title;
                            txtLocation.Text = location;
                            txtContent.Text = content;
                            Calendar1.SelectedDate = start;
                            txtDateTime.Text = start.ToShortDateString();
                            txtHour.Text = start.Hour.ToString();
                            txtMinute.Text = start.Minute.ToString();
                            txtHourEnd.Text = end.Hour.ToString();
                            txtMinuteEnd.Text = end.Minute.ToString();

                            // fileupload thi ko can 
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/LoginMessage.aspx?id=1");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx?id=2");
            }
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        DateTime date = Calendar1.SelectedDate;
        txtDateTime.Text = date.ToString("dd-MM-yyyy");
        Calendar1.Visible = false;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtTitle.Text = "";
        txtDateTime.Text = "";
        txtLocation.Text = "";
        txtContent.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        //Lay id truyen tu querry string
        int id = Convert.ToInt32(Request.QueryString["id"].ToString());
        
        string title = txtTitle.Text;
        string location = txtLocation.Text;
        string content = txtContent.Text;

        int hour = Convert.ToInt32(txtHour.Text);
        int minute = Convert.ToInt32(txtMinute.Text);

        DateTime datestart = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hour, minute, 0);

        //Lay filePath mac dinh la image tren session da luu o tren(Page_Load())
        string filePath = Session["fileUpload"].ToString();

        int hourEnd = Convert.ToInt32(txtHourEnd.Text);
        int minuteEnd = Convert.ToInt32(txtMinuteEnd.Text);
        Response.Write(location);

        DateTime dateEnd = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hourEnd, minuteEnd, 0);

        // Đoạn lệnh này kiểm tra có file trong fileUpload ko
        if (fuImage.HasFile)
        {
            CheckValid cv = new CheckValid();

            // kiểm tra xem file up lên có phải dạng ảnh ko(tránh virus)
            if (cv.CheckFileType(fuImage.FileName))
            {
                filePath = "~/Image/" + fuImage.FileName;

                // kiểm tra xem có tồn tại đường dẫn file này trên server ko
                if (System.IO.File.Exists(MapPath(filePath)))
                {
                    int counter = 2;

                    string tempFileName = "";

                    while (System.IO.File.Exists(MapPath(filePath)))
                    {
                        tempFileName = counter.ToString() + fuImage.FileName;

                        filePath = "~/Image/" + tempFileName;

                        counter++;
                    }
                }

                fuImage.SaveAs(MapPath(filePath));
            }
        }

        CheckValid cvl = new CheckValid();

        if (cvl.checkDate(datestart) == 1)
        {
            if (hourEnd > hour)
            {
                // Vào MaintainMessage.aspx để xem thông báo
                if (!udm.updateEvents(udm.createConnection(), id, title, datestart,dateEnd, location, filePath, content))
                {
                    Response.Redirect("MaintainMessage.aspx?ID=3");
                }
                else
                {
                    Response.Redirect("MaintainMessage.aspx?ID=2");
                }
            }
        }     
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        CheckValid cv = new CheckValid();

        int hour = Convert.ToInt32(txtHour.Text);
        int minute = Convert.ToInt32(txtMinute.Text);

        DateTime datestart = new DateTime(Calendar1.SelectedDate.Year, Calendar1.SelectedDate.Month, Calendar1.SelectedDate.Day, hour, minute, 0);

        if (cv.checkDate(datestart) == 1)
        {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        int hourStart = Convert.ToInt32(txtHour.Text);
        int hourEnd = Convert.ToInt32(txtHourEnd.Text);

        if (hourEnd > hourStart)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
}